package com.jwthhealth_pub.old.physicalfitness.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.physicalfitness.model.PhysicalAnswerModel;
import com.jwthhealth_pub.old.physicalfitness.view.PhysicalFitnessActivity;
import com.jwthhealth_pub.old.physicalfitness.view.PhysicalFitnessAnswerActivity;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/20.
 */
public class phesicalFitnessAdapter extends RecyclerView.Adapter {

    public static final String PHYSICALFINESSANSWER_NO = "0";//否
    public static final String PHYSICALFINESSANSWER_YES = "1";//是

    private Context mContext;
    private PhysicalFitnessActivity mActivity;
    private List<Integer> answers;

    public List<Integer> getAnswers() {
        return answers;
    }

    public phesicalFitnessAdapter(PhysicalFitnessActivity physicalFitnessActivity) {
        this.mContext = physicalFitnessActivity;
        this.mActivity = physicalFitnessActivity;
        answers = new ArrayList<>(7);
        for (int i=0; i<7; i++) {
            answers.add(i, -1);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE.ITEM_DESCRIBE.ordinal()) {
            return new DescrbeViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_physical_describe, parent, false));
        } else if (viewType == ITEM_TYPE.ITEM_FOOT.ordinal()) {
            return new FootViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_physical_foot, parent, false));
        } else {
            return new QuestionViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_physical_question, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        DescrbeViewHolder descrbeViewHolder = null;
        FootViewHolder footViewHolder = null;
        QuestionViewHolder questionViewHolder = null;

        if (position == 0 || position == 1) {
            descrbeViewHolder = (DescrbeViewHolder) holder;
        } else if (position == 9){
            footViewHolder = (FootViewHolder) holder;
        } else {
            questionViewHolder = (QuestionViewHolder) holder;

            questionViewHolder.questionRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    if (i == R.id.item_rb_yes) {
                        answers.set(position-2, 1);
                    } else {
                        answers.set(position-2, 0);
                    }
                }
            });

        }

        switch (position) {
            case 0:
                descrbeViewHolder.describeTv.setText(mContext.getResources().getString(R.string.physicalfiness_describe_one));
                break;
            case 1:
                descrbeViewHolder.describeTv.setText(mContext.getResources().getString(R.string.physicalfiness_describe_two));
                break;
            case 2:
                questionViewHolder.indexTv.setText("1");
                questionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.physicalfiness_question_one));
                break;
            case 3:
                questionViewHolder.indexTv.setText("2");
                questionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.physicalfiness_question_two));
                break;
            case 4:
                questionViewHolder.indexTv.setText("3");
                questionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.physicalfiness_question_thr));
                break;
            case 5:
                questionViewHolder.indexTv.setText("4");
                questionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.physicalfiness_question_for));
                break;
            case 6:
                questionViewHolder.indexTv.setText("5");
                questionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.physicalfiness_question_fiv));
                break;
            case 7:
                questionViewHolder.indexTv.setText("6");
                questionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.physicalfiness_question_six));
                break;
            case 8:
                questionViewHolder.indexTv.setText("7");
                questionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.physicalfiness_question_sev));
                break;
            case 9:
                footViewHolder.commitBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        for (int i=0; i<7; i++) {
                            if (answers.get(i) == -1) {
                                Toast.makeText(mContext, mContext.getResources().getString(R.string.physicalfiness_alert), Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }
                        // TODO: 2016/12/20 提交操作 跳转到下一个界面
                        String answer = "";
                        for (int i=0; i<7; i++) {
                            answer = answer + answers.get(i) + ",";
                        }
                        answer = answer.substring(0, answer.length()-1);

                        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                        Call<PhysicalAnswerModel> constitutionCompleteModelCall;
                        if (userinfoModel != null) {
                            constitutionCompleteModelCall = ApiHelper.physicalCommit(userinfoModel.getId(), answer, userinfoModel.getAndroidtoken());
                        } else {
                            constitutionCompleteModelCall = ApiHelper.physicalCommit(null, answer, null);
                        }

                        constitutionCompleteModelCall.enqueue(new Callback<PhysicalAnswerModel>() {
                            @Override
                            public void onResponse(Call<PhysicalAnswerModel> call, Response<PhysicalAnswerModel> response) {
                                final PhysicalAnswerModel physicalAnswerModel = response.body();
                                if (physicalAnswerModel.getCode().equals("0")){
                                    Intent intent = new Intent(mContext, PhysicalFitnessAnswerActivity.class);
                                    if (physicalAnswerModel.getData().getResult().equals(PHYSICALFINESSANSWER_NO)) {
                                        intent.putExtra(Constant.PHYSICAL_ANSERT, false);
                                    } else {
                                        intent.putExtra(Constant.PHYSICAL_ANSERT, true);
                                    }
                                    mContext.startActivity(intent);
                                    ((PhysicalFitnessActivity) mContext).finish();
                                } else {
                                    mActivity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(mActivity, physicalAnswerModel.getMsg(), Toast.LENGTH_SHORT).show();
                                            mActivity.progressBar(View.GONE);
                                        }
                                    });
                                }
                            }

                            @Override
                            public void onFailure(Call<PhysicalAnswerModel> call, Throwable t) {

                            }
                        });
                        ((PhysicalFitnessActivity) mContext).progressbar();
                    }
                });
                break;
            default:break;
        }



    }

    @Override
    public int getItemCount() {
        return 10;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == 1) {
            return  ITEM_TYPE.ITEM_DESCRIBE.ordinal();
        } else if (position == 9) {
            return ITEM_TYPE.ITEM_FOOT.ordinal();
        } else {
            return ITEM_TYPE.ITEM_QUESTION.ordinal();
        }
    }

    private class DescrbeViewHolder extends RecyclerView.ViewHolder{

        TextView describeTv;

        public DescrbeViewHolder(View itemView) {
            super(itemView);
            describeTv = (TextView) itemView.findViewById(R.id.item_describe);
        }
    }

    class QuestionViewHolder extends RecyclerView.ViewHolder{

        TextView questionTv, indexTv;
        RadioGroup questionRg;

        public QuestionViewHolder(View itemView) {
            super(itemView);
            indexTv = (TextView) itemView.findViewById(R.id.tv_index);
            questionTv = (TextView) itemView.findViewById(R.id.item_describe);
            questionRg = (RadioGroup) itemView.findViewById(R.id.psysical_rg);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        Button commitBtn;

        public FootViewHolder(View itemView) {
            super(itemView);
            commitBtn = (Button) itemView.findViewById(R.id.btn_physical_commit);
        }
    }

    private enum ITEM_TYPE {
        ITEM_DESCRIBE, ITEM_QUESTION, ITEM_FOOT;
    }
}
